(function(){
// 回到首页
	var goback = document.querySelector("header>span:first-child");
	goback.addEventListener("click",function(){ open("../index.html") });
//取消优惠信息
	var unshow = document.querySelector(".xiaoshi");
	unshow.addEventListener("click", function(){ this.parentNode.remove() });
// 购物车金额计算
// 数据模拟
	var shopArr = [
		{"imgsrc":"../img/mv2.jpg","redu":"货品介绍","nowprice":"￥16.5","goprice":"￥24"},
		{"imgsrc":"../img/mv5.jpg","redu":"货品介绍","nowprice":"￥16.5","goprice":"￥24"},
		{"imgsrc":"../img/buy6.jpg","redu":"货品介绍","nowprice":"￥16.5","goprice":"￥24"},
	];
	function shopcart(){
		this.parent = document.querySelector("section");
		this.resultnum = document.querySelector("footer>div:last-child");
		this.totalpr = document.querySelector("footer>div>span:last-child>b");
		this.allcheck = document.querySelector("footer>div:first-child>span:first-child>i");
	}
	shopcart.prototype.init = function(){
		this.render();
		this.bind();
	}
	shopcart.prototype.render= function(){
		shopArr.forEach(val=>{
			var shopcontainer = document.createElement("div"),
			checkcontainer = document.createElement("div"),
			checkspan = document.createElement("span"),
			imgcontainer = document.createElement("div"),
			img = new Image();
			img.src = val.imgsrc;
			checkspan.className = "iconfont icon-gouxuan";
			checkcontainer.className = "ischecked";
			checkcontainer.appendChild(checkspan);
			imgcontainer.appendChild(img);
			var pcontainer = document.createElement("div"),
			p1 = document.createElement("p"),
			p2 = document.createElement("p"),
			p3 = document.createElement("p"),
			p2span1 = document.createElement("span"),
			p2span2 = document.createElement("span"),
			p3span1 = document.createElement("span"),
			p3span2 = document.createElement("span");
			p1.innerText = val.redu;
			p2span1.innerText = val.nowprice;
			p2span2.innerText = val.goprice;
			p3span1.innerText = "小计";
			p3span2.innerText = val.nowprice;
			p2.appendChild(p2span1);
			p2.appendChild(p2span2);
			p3.appendChild(p3span1);
			p3.appendChild(p3span2);
			pcontainer.appendChild(p1);
			pcontainer.appendChild(p2);
			pcontainer.appendChild(p3);
			var btncontainer = document.createElement("div"),
			btnspan1 = document.createElement("span"),
			numspan = document.createElement("span"),
			btnspan2 = document.createElement("span");
			btnspan1.className = "iconfont icon-jian";
			numspan.innerText = 1;
			btnspan2.className = "iconfont icon-icon-";
			btncontainer.appendChild(btnspan1);
			btncontainer.appendChild(numspan);
			btncontainer.appendChild(btnspan2);
			shopcontainer.appendChild(checkcontainer);
			shopcontainer.appendChild(imgcontainer);
			shopcontainer.appendChild(pcontainer);
			shopcontainer.appendChild(btncontainer);
			this.parent.appendChild(shopcontainer);
		})
	}
	shopcart.prototype.bind = function(){
		this.checkeds = document.querySelectorAll(".ischecked");
		var me = this;
		this.parent.onclick = function(){
			let target = event.srcElement;
			if(target.className == "ischecked"){
				target.style.background = target.style.background == "rgb(251, 71, 65)" ? "" : "rgb(251, 71, 65)";
			}else if(target.className == "iconfont icon-jian"){
				if(target.nextElementSibling.innerText > 0){
					target.nextElementSibling.innerText--;
					target.parentNode.previousElementSibling.lastElementChild.lastElementChild.innerText = "￥" +
					target.parentNode.previousElementSibling.lastElementChild.previousElementSibling.firstElementChild.innerText.slice(1)*
					target.nextElementSibling.innerText;
				}
			}else if(target.className == "iconfont icon-icon-"){
				target.previousElementSibling.innerText++;
				target.parentNode.previousElementSibling.lastElementChild.lastElementChild.innerText = "￥" +
				target.parentNode.previousElementSibling.lastElementChild.previousElementSibling.firstElementChild.innerText.slice(1)*
				target.previousElementSibling.innerText;
			}
			let num = 0,totalprice = 0,helpnum = 6;
			me.checkeds.forEach(val=>{
				if(val.style.background == "rgb(251, 71, 65)"){
					num += parseInt(val.parentNode.lastElementChild.children[1].innerText);
					totalprice += parseFloat(val.nextElementSibling.nextElementSibling.lastElementChild.lastElementChild.innerText.slice(1));
				}else{
					helpnum = 0;
				}
			})
			me.resultnum.innerText = "结算 ( "+ num + " )";
			me.totalpr.innerText = "￥" + totalprice;
			helpnum == 0 ? me.allcheck.style.background = "" : me.allcheck.style.background = "rgb(251, 71, 65)";
		}
		this.allcheck.onclick = function(){
			if(this.style.background == ""){
				this.style.background = "rgb(251, 71, 65)";
				me.checkeds.forEach(val=>val.style.background="rgb(251, 71, 65)");
			}else{
				this.style.background = "";
				me.checkeds.forEach(val=>val.style.background="");
			}
			me.parent.onclick();
		}
	}
	var mycart = new shopcart();
	mycart.init();
})()